package com.fast.fastenjoy.base.utils;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Set;

import com.opensymphony.xwork2.util.ResolverUtil;

/**
 * class或包工具类
 * @author chenliaohua
 *
 */
public class PackageUtils {

	public static Type getGenericEntityClass(Class<?> clazz) {
		
		if ((clazz == null) || (Object.class.equals(clazz))) {
			return null;
		}
		
		Type gsc = clazz.getGenericSuperclass();
		
		if (gsc instanceof ParameterizedType) {
			ParameterizedType pt = (ParameterizedType) gsc;
			Type[] atas = pt.getActualTypeArguments();
			if (atas.length > 0) {
				return atas[0];
			}
		}
		
		Class<?> superclass = clazz.getSuperclass();
		
		return getGenericEntityClass(superclass);
	}

	/**
	 * 获取包下面所有包含的对应注解的class
	 * 
	 * @param annotationClass
	 * @param packageNameList
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Set<Class<?>> getClassName(Class<?> annotationClass,
			List<String> packageNameList) {

		if (annotationClass == null) {
			return null;
		}

		ResolverUtil ru = new ResolverUtil();
		
		String[] packageNames = new String[packageNameList.size()];

		packageNames = packageNameList.toArray(packageNames);

		ru.findAnnotated(annotationClass, packageNames);

		return ru.getClasses();
	}

}